package cn.wk.libs4a.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.wk.libs4a.R;

/**
 * 公共标题栏，中间搜索或者文本，两边按钮
 */
public class WKHeader extends LinearLayout {

	public TextView leftTxt;
	public TextView rightTxt;

	public ImageButton leftImg;
	public ImageButton rightImg;

	public TextView title;
	public ImageView searchBtn;
	public TextView head_search_txt;

	public ImageView header_line;

	public AutoCompleteTextView search_edit;

	public View search_view;

	public ImageView clear_btn;

	// 返回按钮，提前配置
	public static int HEADER_BACK_ICO_ID = R.drawable.header_back_btn;
	// 标题颜色，需要提前配置
	public static int HEADER_BG_COLOR = R.color.gary;

	public static int HEADER_TITLE_COLOR = R.color.tab_text;

	public static int HEADER_RIGHT_TXT_COLOR = R.color.tab_text;

	// public static float HEADER_TITLE_SIZE = R.dimen.wk_item_title_big_size;

	public WKHeader(Context context) {
		this(context, null);
	}

	public WKHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		addView(View.inflate(context, R.layout.wk_header, null),
				new LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.MATCH_PARENT));
		initView();
		loadParams(attrs);
	}

	private void initView() {
		leftTxt = (TextView) findViewById(R.id.head_left_txt);
		leftImg = (ImageButton) findViewById(R.id.head_left_img);
		rightTxt = (TextView) findViewById(R.id.head_right_txt);
		rightImg = (ImageButton) findViewById(R.id.head_right_img);
		title = (TextView) findViewById(R.id.head_center_title);
		searchBtn = (ImageView) findViewById(R.id.head_search);
		head_search_txt = (TextView) findViewById(R.id.head_search_txt);
		search_edit = (AutoCompleteTextView) findViewById(R.id.search_edit_2);

		search_view = findViewById(R.id.search_view);

		clear_btn = (ImageView) findViewById(R.id.clear_btn);
		View bg = findViewById(R.id.root_view);
		bg.setBackgroundColor(getResources().getColor(HEADER_BG_COLOR));

		title.setTextColor(getResources().getColor(HEADER_TITLE_COLOR));
		rightTxt.setTextColor(getResources().getColor(HEADER_RIGHT_TXT_COLOR));

		// title.setTextSize(HEADER_TITLE_SIZE);
	}

	private void loadParams(AttributeSet attrs) {
		TypedArray ta = this.getContext().obtainStyledAttributes(attrs,
				R.styleable.WKHeader);
		String title = ta.getString(R.styleable.WKHeader_title);
		setTitle(title);

		String left = ta.getString(R.styleable.WKHeader_leftTxt);
		if (left != null) {
			this.leftTxt.setText(left);
		}
		String right = ta.getString(R.styleable.WKHeader_rightTxt);
		if (right != null) {
			this.rightTxt.setText(right);
		}
		Drawable titleImg = ta.getDrawable(R.styleable.WKHeader_titleimg);
		if (titleImg != null) {
			this.searchBtn.setImageDrawable(titleImg);
		}

		Boolean leftAsBack = ta.getBoolean(R.styleable.WKHeader_leftAsBack,
				false);
		if (leftAsBack) {
			setLeftAsBack();
		}
		ta.recycle();
	}

	/**
	 * 设置左边返回按钮
	 */
	public WKHeader setLeftAsBack() {
		leftImg.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Context ctx = WKHeader.this.getContext();
				if (ctx instanceof Activity) {
					((Activity) ctx).onBackPressed();
				}
			}
		});
		leftTxt.setText("");
		leftImg.setImageResource(HEADER_BACK_ICO_ID);
		return this;
	}

	/**
	 * 设置右边文字
	 */
	public void setRightTxt(String righttxt) {
		rightTxt.setText(righttxt);
	}

	/**
	 * 设置标题
	 */
	public WKHeader setTitle(String t) {
		if (t == null || t.equals("")) {
			title.setVisibility(View.GONE);
			search_view.setVisibility(View.VISIBLE);
		} else if (t.equals("search_edit")) {
			title.setVisibility(View.GONE);
			search_view.setVisibility(View.VISIBLE);
			head_search_txt.setVisibility(View.GONE);
			search_edit.setVisibility(View.VISIBLE);
		} else {
			title.setVisibility(View.VISIBLE);
			search_view.setVisibility(View.GONE);
			title.setText(t);
		}
		return this;
	}

	/**
	 * 初始化搜索相关
	 */
	public void initSearchView() {
		title.setVisibility(View.GONE);
		search_view.setVisibility(View.VISIBLE);
		head_search_txt.setVisibility(View.GONE);
		search_edit.setVisibility(View.VISIBLE);

		clear_btn.setVisibility(View.GONE);
		clear_btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				search_edit.setText("");
			}
		});

		search_edit.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String ss = s.toString();
				if (ss == null || ss.equals("")) {
					clear_btn.setVisibility(View.GONE);
				} else {
					clear_btn.setVisibility(View.VISIBLE);
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});

	}

	/**
	 * 设置headerinfo的点击事件
	 */
	public WKHeader setClickListener(OnClickListener left,
			OnClickListener center, OnClickListener right) {
		if (center != null) {
			title.setOnClickListener(center);
			head_search_txt.setOnClickListener(center);
		}
		if (left != null) {
			leftImg.setOnClickListener(left);
		}
		if (right != null) {
			rightImg.setOnClickListener(right);
		}
		return this;
	}

	/**
	 * 获取左按钮
	 */
	public TextView getLeftTxt() {
		return leftTxt;
	}

	/**
	 * 获取右按钮
	 */
	public TextView getRightTxt() {
		return rightTxt;
	}

	/**
	 * 设置标题点击事件
	 */
	public WKHeader setTitleClick(OnClickListener click) {
		title.setOnClickListener(click);
		return this;
	}

	/**
	 * 设置AutoCompleteTextView的适配器
	 */
	public WKHeader setAutoCompleteAdapter(Context context, int layoutId,
			String[] items) {
		search_edit.setAdapter(new ArrayAdapter<String>(context, layoutId,
				items));
		return this;
	}

	/**
	 * 设置按钮左右图片（drawableTop），boolean值为真则为左侧，为假则为右侧
	 */
	@SuppressLint("NewApi")
	public WKHeader setLeftImg(String txt, int id, boolean left, float size) {
		Drawable img = getResources().getDrawable(id);
		// 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
		img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
		if (left) {
			if (txt != null) {
				leftTxt.setTextSize(size);
				leftTxt.setText(txt);
				leftTxt.setCompoundDrawables(null, img, null, null);
			} else {
				leftTxt.setText("");
				leftImg.setImageDrawable(img);
			}

		} else {
			if (txt != null) {
				rightTxt.setTextSize(size);
				rightTxt.setText(txt);
				rightTxt.setCompoundDrawables(null, img, null, null);
			} else {
				rightTxt.setText("");
				rightImg.setImageDrawable(img);
			}
		}
		return this;
	}

	public WKHeader setBackColor(int id) {
		setBackgroundColor(getResources().getColor(id));
		return this;
	}

}
